package com.joekai.view_demo.shape_loading;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class ShapeView extends View {

    private ShapeType mCurrentShape = ShapeType.Circular;//默认圆形
    private Paint mPaint;
    private Thread thread;
    private Path mPath;

    public ShapeView(Context context) {
        this(context,null);
    }

    public ShapeView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ShapeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr,0);
    }

    public ShapeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获取宽高的模式
        int widgetMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        //获取宽高的大小
        int widgetSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(widgetSize > heightSize ? heightSize : widgetSize, widgetSize > heightSize ? heightSize : widgetSize);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        switch (mCurrentShape) {
            //圆形
            case Circular:
                //获取半径
                int center = getWidth() / 2;
                mPaint.setColor(Color.YELLOW);
                canvas.drawCircle(center, center, center, mPaint);
                break;
            //正方形
            case Square:
                mPaint.setColor(Color.BLUE);
                canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);
                break;
            //三角形
            case Triangle:
                mPaint.setColor(Color.GRAY);
                if (mPath == null) {
                    mPath = new Path();
                    mPath.setFillType(Path.FillType.EVEN_ODD);
                    mPath.moveTo(getWidth() / 2, 0);
                    mPath.lineTo(0, (float) (getWidth() / 2 * Math.sqrt(3)));
                    mPath.lineTo(getWidth(), (float) (getWidth() / 2 * Math.sqrt(3)));
                    mPath.close();//线路闭合
                }
                canvas.drawPath(mPath, mPaint);
                break;
        }
    }

    public synchronized void start(final Activity activity) {
        thread = new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        while (true) {
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    changeShape();
                                }
                            });
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }

                    }
                }
        );
        thread.start();
    }

    public synchronized void stop() {
        if (thread != null || thread.isAlive()) {
            thread = null;
        }
    }

    public void changeShape() {
        switch (mCurrentShape) {
            //圆形
            case Circular:
                mCurrentShape = ShapeType.Square;
                break;
            //正方形
            case Square:
                mCurrentShape = ShapeType.Triangle;
                break;
            //三角形
            case Triangle:
                mCurrentShape = ShapeType.Circular;
                break;
        }
        invalidate();
    }

    public ShapeType getCurrentShape() {
        return  mCurrentShape;
    }

}
